/**
 * JAVACC DEMO 1.0
 */
package com.apache.client.common;

import com.apache.method.SystemRunnable;
import com.apache.method.ThreadPool;

/**
 * description:  日志线程池启动控制类
 *
 * @author Hou Dayu 创建时间：2016-1-14
 */
public class ScheduledExecutor {

    private static ScheduledExecutor instance;

    private int poolNum = 3;

    private ThreadPool taskPool;

    private ScheduledExecutor() {
    }

    /**
     * description:  创建单实例方法
     *
     * @return
     */
    public static ScheduledExecutor newInstance() {
        if (null == instance) {
            instance = new ScheduledExecutor();
        }
        return instance;
    }

    public synchronized void saveLogger(SystemRunnable runnable) {
        if (null != runnable) {
            if(taskPool==null){
                taskPool = new ThreadPool(poolNum, "Logger执行线程池");
            }
            taskPool.execute(runnable);
        }
    }

    public void init() {
        instance = this;
    }

    /**
     * description:  销毁方法
     */
    public void destroy() {
        if(taskPool != null) {
            taskPool.closePool();
            taskPool = null;
        }
        instance = null;
    }

    public void setPoolNum(int poolNum) {
        this.poolNum = poolNum;
    }

    private void initTaskPool(){
        taskPool = new ThreadPool(poolNum, "Logger执行线程池");
    }
}
